const app = getApp();
const {upTime} = require('/util/public');
Page({
    data: {
        query:'',
        productOrderRecordVoList:[]
    },
    onLoad(query) {
        let id = query.param;
        this.setData({query});
        this.getoOrderBill(id);
    },

    // 获取账单数据
    getoOrderBill(id){
        const time = new Date().setHours(0,0,0,0)/1000+(24*60*60);
        console.log((time));
        app.order.detail(id)
        .then(res=>{
            console.log(res)
            let productOrderRecordVoList = res.data.productOrderRecordVoList;
            productOrderRecordVoList.forEach((item,index)=>{
                console.log(item)
                item.showBtn = false;
                if(time>item.expiretime) {
                    item.showBtn = true
                };
                item.time = upTime("%Y/%M/%D",time);
                item.createtime = upTime("%Y/%M/%D",item.createtime);
                item.duetime = upTime("%Y/%M/%D",item.duetime);
                item.expiretime = upTime("%Y/%M/%D",item.expiretime);
                item.price = (item.price/100).toFixed(2);
            })
            this.setData({
                productOrderRecordVoList
            })
        })
    },

    // 支付账单
    async onpay(e){
        const oid = e.target.dataset.param.oid,
        id = e.target.dataset.param.id,
        index = e.target.dataset.param.index;
        const res = await app.order.alipay(oid,id);
        // console.log(res);
        const pay = await app.alipay(res.data);
        // console.log(pay);
        if(pay.resultCode == 9000) {
            let productOrderRecordVoList = this.data.productOrderRecordVoList;
            productOrderRecordVoList[index].payment = 1;
            this.setData({productOrderRecordVoList});
        };
    },

    
    // 下拉刷新
    onPullDownRefresh() {
        this.onLoad(this.data.query);
        setTimeout(() => {
            my.stopPullDownRefresh({});
        }, 1000)
    },
    stopPullDownRefresh() {
        my.stopPullDownRefresh({});
    }
});
